JavaScript

A5.u.arrayexplode Method

Syntax

A5.u.array.explode(array,targets)

Arguments

arrayarray

The array to explode.

targetsstringobjectarray

The target child arrays to explode.

targetstring

The property name of an array.

toobject

Settings for the mapping of values out of the child array into the parent object.

typestring

The type of explosion to perform. Values can be "prefix" (the default), which will expand any child objects in the array to be top level properties with a prefix. A value of "object" will place the contents of the array in a single property in the parent.

valuestring

The value to use for either the property prefix or object name. By default the "target" value will be used.

childrenbooleanarray

Child array(s) to explode. Contents of the array can be the same as the top level "targets" argument.

arrayarray

An exploded array.

Description

Explode child arrays in an array of object.

Discussion

The A5.u.array.explode method is used to explode child arrays in an array of objects. Targets for explosion are defined by either a string with the property name, or an object. When an object is passed in for the target, further control over the resulting array is allowed. This includes whether or not to prefix the properties of the child array entries, or place them in a single object, as well as the value of the prefix or property name. An object must also be supplied if recursive explosions are required. This is done by supplying a "children" targets array in the target.

Example

var arr = [
		{
			"Name": "John Smith",
			"City" : "Boston",
			"State" : "MA",
			"Children": [{"Name" : "Callie", "Age" : 5,  "Hobbies": ["Drawing"]}, {"Name" : "Griffin", "Age" :3},{"Name" : "Luke", "Age" : 1}],
			"Interests": [{"Name": "Baseball"}, {"Name": "Coding"}]
		}, 
		{
			"Name": "Henry Rhodes",
			"City" : "New York",
			"State" : "NY",
			"Children": [{"Name" : "Howard", "Age" : 15, "Hobbies": ["Hunting","Running"]},{"Name" : "Robert", "Age" : 11, "Hobbies": ["Video games"]}],
			"Interests": [{"Name": "Cooking"}]
		}, 
		{
			"Name": "Allison Berman",
			"City" : "Los Angeles",
			"State" : "CA", "Children": [{"Name" : "Denzel", "Age" : 11, "Hobbies": ["Boxing"]}],
			"Interests": [{"Name": "MMA"}]
		}
	]
var eArr = A5.u.array.explode(arr,[
		{
			target: 'Children',
			to: {type: 'object', value: 'Child'},
			children: ['Hobbies']
		},
		'Interests'
	]);
// eArr = [
//		{"Name":"John Smith","City":"Boston","State":"MA","Child":{"Name":"Callie","Age":5,"Hobbies":"Drawing"}},
//		{"Name":"John Smith","City":"Boston","State":"MA","Child":{"Name":"Griffin","Age":3}},
//		{"Name":"John Smith","City":"Boston","State":"MA","Child":{"Name":"Luke","Age":1}},
//		{"Name":"John Smith","City":"Boston","State":"MA","Child":false,"InterestsName":"Baseball"},
//		{"Name":"John Smith","City":"Boston","State":"MA","Child":false,"InterestsName":"Coding"},
//		{"Name":"Henry Rhodes","City":"New York","State":"NY","Child":{"Name":"Howard","Age":15,"Hobbies":"Hunting"}},
//		{"Name":"Henry Rhodes","City":"New York","State":"NY","Child":{"Name":"Howard","Age":15,"Hobbies":"Running"}},
//		{"Name":"Henry Rhodes","City":"New York","State":"NY","Child":{"Name":"Robert","Age":11,"Hobbies":"Video games"}},
//		{"Name":"Henry Rhodes","City":"New York","State":"NY","Child":false,"InterestsName":"Cooking"},
//		{"Name":"Allison Berman","City":"Los Angeles","State":"CA","Child":{"Name":"Denzel","Age":11,"Hobbies":"Boxing"}},
//		{"Name":"Allison Berman","City":"Los Angeles","State":"CA","Child":false,"InterestsName":"MMA"}
// ]